#include <DOWNLOAD.h>

#if DOWNLOAD_EN

static uint8_t work_flg = 1;
static uint8_t down_count = 0;
static uint8_t down_count_max = download_delay_default;

void download_chek_loop()
{
    if (P30)
    {
        down_count = 0;
    }
    else
    {
        if (work_flg == 1)
        {
            down_count++;
            if (down_count >= down_count_max)
            {
                IAP_CONTR |= sys_wake_up_from_isp;
                IAP_CONTR |= sys_reset_val;
            }
        }
    }
}

void download_disable()
{
    work_flg = 0;
}

void download_enable()
{
    work_flg = 1;
}

void download_delay_set(uint8_t val)
{
    down_count = 0;
    down_count_max = val;
}

void sys_restart()
{
    IAP_CONTR |= sys_wake_up_from_isp;
    IAP_CONTR |= sys_reset_val;
}

#endif
